/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SupplierFrame.java
 *
 * Created on 26 Des 12, 21:46:50
 */
package com.ivanbiz.gui;

import com.ivanbiz.dao.ApproveItemDAO;
import com.ivanbiz.dao.ItemDAO;
import com.ivanbiz.dao.impl.ApproveItemDAOImpl;
import com.ivanbiz.dao.impl.ItemDAOImpl;
import com.ivanbiz.helper.GlobalConstant;
import com.ivanbiz.helper.ServiceHelper;
import com.ivanbiz.helper.UserSession;
import com.ivanbiz.model.ApproveItem;
import com.ivanbiz.model.User;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Administrator
 */
public class ApproveItemFrame extends javax.swing.JInternalFrame {

    ApproveItemDAO approveItemDAO = new ApproveItemDAOImpl();
    ItemDAO itemDAO = new ItemDAOImpl();
    List<ApproveItem> listApproveItem = new ArrayList<ApproveItem>();
    ServiceHelper serviceHelper = new ServiceHelper();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM yyyy HH:mm:ss");
    User user = UserSession.getUser();

    /** Creates new form SupplierFrame */
    public ApproveItemFrame() {
        this.setVisible(true);
        initComponents();
        updateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableData = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        comboSearch = new javax.swing.JComboBox();
        textSearch = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        buttonReset = new javax.swing.JButton();
        buttonApprove = new javax.swing.JButton();

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tabel Data", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Monospaced", 0, 11))); // NOI18N

        tableData.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N
        tableData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableData);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 602, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Search", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Monospaced", 0, 11))); // NOI18N

        comboSearch.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N

        textSearch.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N
        textSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textSearchKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Button", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Monospaced", 0, 11))); // NOI18N

        buttonReset.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N
        buttonReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/gui/icon/Reset.jpg"))); // NOI18N
        buttonReset.setText("Reset");
        buttonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonResetActionPerformed(evt);
            }
        });

        buttonApprove.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N
        buttonApprove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/gui/icon/save.jpg"))); // NOI18N
        buttonApprove.setText("Approve");
        buttonApprove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonApproveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(buttonApprove)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonApprove, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void textSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textSearchKeyReleased
        try {
            listApproveItem = approveItemDAO.getDataByLike(ApproveItem.class, (String) comboSearch.getSelectedItem(), textSearch.getText());
            updateTable();
        } catch (Exception ex) {
            Logger.getLogger(ApproveItemFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_textSearchKeyReleased

    private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetActionPerformed
        updateData();
}//GEN-LAST:event_buttonResetActionPerformed

    private void buttonApproveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonApproveActionPerformed
        try {
            int response = JOptionPane.showConfirmDialog(null, GlobalConstant.PROCCESS, GlobalConstant.CONFIRM, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.YES_OPTION) {
                approveItemDAO.updateOrSave(listApproveItem, tableData, user);
                JOptionPane.showMessageDialog(this, GlobalConstant.MSG_APR);
                updateData();
            }
        } catch (Exception ex) {
            Logger.getLogger(ApproveItemFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_buttonApproveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonApprove;
    private javax.swing.JButton buttonReset;
    private javax.swing.JComboBox comboSearch;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableData;
    private javax.swing.JTextField textSearch;
    // End of variables declaration//GEN-END:variables

    private void updateData() {
        try {
            clearData();
            listApproveItem = approveItemDAO.getDataByEquals(ApproveItem.class, "status", "N");
            updateTable();
            updateCombo();
        } catch (Exception ex) {
            Logger.getLogger(ApproveItemFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void clearData() {
        buttonApprove.setEnabled(true);
        textSearch.setText("");
    }

    private void updateTable() {
        Object[] judul = {GlobalConstant.CHOOSE, GlobalConstant.NO, GlobalConstant.NO_PO, GlobalConstant.KODE_BARCODE, GlobalConstant.NAMA, GlobalConstant.QUANTITY, GlobalConstant.DATE_RECEIVE};
        Object[][] isi = new Object[listApproveItem.size()][7];
        int x = 0;
        int no = 0;
        for (ApproveItem approveItem : listApproveItem) {
            isi[x][0] = approveItem.isCheck();
            isi[x][1] = no += 1;
            isi[x][2] = approveItem.getNoPO();
            isi[x][3] = approveItem.getKodeBarcode();
            isi[x][4] = approveItem.getNamaItem();
            isi[x][5] = approveItem.getQuantity();
            isi[x][6] = simpleDateFormat.format(approveItem.getDateReceive());
            x++;

        }
        tableData.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {

                if (columnIdentifiers.get(columnIndex).equals(GlobalConstant.CHOOSE)) {
                    return true;
                } else {
                    return false;
                }
            }

            @Override
            public Class getColumnClass(int columnIndex) {
                if (columnIdentifiers.get(columnIndex).equals(GlobalConstant.CHOOSE)) {
                    return Boolean.class;
                } else {
                    return columnIdentifiers.get(columnIndex).getClass();
                }
            }
        });

        TableColumn column0 = tableData.getColumnModel().getColumn(0);
        column0.setPreferredWidth(50);
        column0.setMaxWidth(50);
        TableColumn column1 = tableData.getColumnModel().getColumn(1);
        column1.setPreferredWidth(75);
        column1.setMaxWidth(75);
        TableColumn column2 = tableData.getColumnModel().getColumn(2);
        column2.setPreferredWidth(150);
        column2.setMaxWidth(150);
        TableColumn column3 = tableData.getColumnModel().getColumn(3);
        column3.setPreferredWidth(150);
        column3.setMaxWidth(150);
        TableColumn column5 = tableData.getColumnModel().getColumn(5);
        column5.setPreferredWidth(75);
        column5.setMaxWidth(75);
        TableColumn column6 = tableData.getColumnModel().getColumn(6);
        column6.setPreferredWidth(180);
        column6.setMaxWidth(180);
    }

    private void updateCombo() {
        Object[] dataClass = serviceHelper.getPropertyClass(ApproveItem.class);
        Object[] dataSearch = new Object[2];
        dataSearch[0] = dataClass[1];
        dataSearch[1] = dataClass[2];
        comboSearch.setModel(new DefaultComboBoxModel(dataSearch));
    }
}
